home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / Java2D / TextureChooser.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  6.3 KB  |  198 lines

  1. /*
  2.  * @(#)TextureChooser.java    1.18 98/09/13
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16. import java.awt.*;
  17. import java.awt.geom.Ellipse2D;
  18. import java.awt.image.BufferedImage;
  19. import java.awt.event.MouseListener;
  20. import java.awt.event.MouseEvent;
  21. import java.awt.font.TextLayout;
  22. import java.awt.font.FontRenderContext;
  23. import javax.swing.JPanel;
  24. import javax.swing.border.TitledBorder;
  25. import javax.swing.border.EtchedBorder;
  26. import java.awt.event.WindowEvent;
  27. import java.awt.event.WindowAdapter;
  28. import java.net.URL;
  29.  
  30.  
  31. /**
  32.  * Four types of Paint displayed: Geometry, Text & Image Textures and
  33.  * a Gradient Paint.  Paints can be selected with the Mouse.
  34.  */
  35. public class TextureChooser extends JPanel {
  36.  
  37.     static public Object texture = getGeomTexture();
  38.     public int num;
  39.  
  40.     public TextureChooser(int num) {
  41.         this.num = num;
  42.         setLayout(new GridLayout(0,2,5,5));
  43.         setBorder(new TitledBorder(new EtchedBorder(), "Texture Chooser"));
  44.  
  45.         add(new ChooserComponent(getGeomTexture(), this, 0));
  46.         add(new ChooserComponent(getImageTexture(), this, 1));
  47.         add(new ChooserComponent(getTextTexture(), this, 2));
  48.         add(new ChooserComponent(getGradientPaint(), this, 3));
  49.     }
  50.  
  51.  
  52.     static public TexturePaint getGeomTexture() {
  53.         BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
  54.         Graphics2D tG2 = bi.createGraphics();
  55.         tG2.setBackground(Color.white);
  56.         tG2.clearRect(0,0,5,5);
  57.         tG2.setColor(new Color(211,211,211,200));
  58.         tG2.fill(new Ellipse2D.Float(0,0,5,5));
  59.         Rectangle r = new Rectangle(0,0,5,5);
  60.         return new TexturePaint(bi,r);
  61.     }
  62.  
  63.     public TexturePaint getImageTexture() {
  64.         URL url = TextureChooser.class.getResource("images/HotJava-16.gif");
  65.         Image img = getToolkit().getImage(url);
  66.         try {
  67.             MediaTracker tracker = new MediaTracker(this);
  68.             tracker.addImage(img, 0);
  69.             tracker.waitForID(0);
  70.         } catch (Exception e) {}
  71.         int iw = img.getWidth(this);
  72.         int ih = img.getHeight(this);
  73.         BufferedImage bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
  74.         Graphics2D tG2 = bi.createGraphics();
  75.         tG2.drawImage(img, 0, 0, this);
  76.         Rectangle r = new Rectangle(0,0,iw,ih);
  77.         return new TexturePaint(bi,r);
  78.     }
  79.  
  80.  
  81.     public TexturePaint getTextTexture() {
  82.         Font f = new Font("Times New Roman", Font.BOLD, 10);
  83.         TextLayout tl = new TextLayout("Java2D", f, new FontRenderContext(null, false, false));
  84.         int sw = (int) tl.getBounds().getWidth();
  85.         int sh = (int) (tl.getAscent()+tl.getDescent());
  86.         BufferedImage bi = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB);
  87.         Graphics2D tG2 = bi.createGraphics();
  88.         tG2.setBackground(Color.white);
  89.         tG2.clearRect(0,0,sw,sh);
  90.         tG2.setColor(Color.lightGray);
  91.         tl.draw(tG2, 0, (float) tl.getAscent());
  92.         Rectangle r = new Rectangle(0,0,sw,sh);
  93.         return new TexturePaint(bi,r);
  94.     }
  95.  
  96.  
  97.     public GradientPaint getGradientPaint() {
  98.         return new GradientPaint(0,0,Color.white,80,0,Color.green);
  99.     }
  100.  
  101.     public class ChooserComponent extends JPanel implements MouseListener {
  102.  
  103.         public boolean clickedFrame;
  104.         private int num;
  105.         private TextureChooser tc;
  106.         private boolean enterExitFrame = false;
  107.         private Object t;
  108.  
  109.         public ChooserComponent(Object t, TextureChooser tc, int num) {
  110.             setBackground(Color.white);
  111.             this.t = t;
  112.             this.tc = tc;
  113.             this.clickedFrame = (num == tc.num);
  114.             this.num = num;
  115.             if (num == tc.num)
  116.                 tc.texture = t;
  117.             addMouseListener(this);
  118.         }
  119.  
  120.         public void paintComponent(Graphics g) {
  121.             super.paintComponent(g);
  122.             Graphics2D g2 = (Graphics2D) g;
  123.             int w = getSize().width;
  124.             int h = getSize().height;
  125.             if (t instanceof TexturePaint)
  126.                 g2.setPaint((TexturePaint) t);
  127.             else {
  128.                 g2.setPaint((GradientPaint) t);
  129.             }
  130.             g2.fill(new Rectangle(0,0,w,h));
  131.             if (clickedFrame || enterExitFrame) {
  132.                 g2.setColor(Color.gray);
  133.                 BasicStroke bs = new BasicStroke(3, BasicStroke.CAP_BUTT,
  134.                                 BasicStroke.JOIN_MITER);
  135.                 g2.setStroke(bs);
  136.                 g2.drawRect(0,0,w-1,h-1);
  137.                 tc.num = num;
  138.             }
  139.         }
  140.  
  141.         public void mouseClicked(MouseEvent e) {
  142.             tc.texture = t;
  143.             clickedFrame = true;
  144.  
  145.             Component cmps[] = tc.getComponents();
  146.             for (int i = 0; i < cmps.length; i++) {
  147.                 if (cmps[i] instanceof ChooserComponent) {
  148.                     ChooserComponent cc = (ChooserComponent) cmps[i];
  149.                     if (!cc.equals(this) && cc.clickedFrame) {
  150.                         cc.clickedFrame = false;
  151.                         cc.repaint();
  152.                     }
  153.                 }
  154.             }
  155.         }
  156.  
  157.         public void mousePressed(MouseEvent e) {
  158.         }
  159.  
  160.         public void mouseReleased(MouseEvent e) {
  161.         }
  162.  
  163.         public void mouseEntered(MouseEvent e) {
  164.             enterExitFrame = true;
  165.             repaint();
  166.         }
  167.  
  168.         public void mouseExited(MouseEvent e) {
  169.             enterExitFrame = false;
  170.             repaint();
  171.         }
  172.  
  173.         public Dimension getMinimumSize() {
  174.             return getPreferredSize();
  175.         }
  176.  
  177.         public Dimension getMaximumSize() {
  178.             return getPreferredSize();
  179.         }
  180.  
  181.         public Dimension getPreferredSize() {
  182.             return new Dimension(30,30);
  183.         }
  184.  
  185.     }
  186.  
  187.     public static void main(String s[]) {
  188.         Frame f = new Frame("Java2D Demo - TextureChooser");
  189.         f.addWindowListener(new WindowAdapter() {
  190.             public void windowClosing(WindowEvent e) {System.exit(0);}
  191.         });
  192.         f.add("Center", new TextureChooser(0));
  193.         f.pack();
  194.         f.setSize(new Dimension(400,400));
  195.         f.show();
  196.     }
  197. }
  198.